package org.example.benchmarkTest;

import org.example.config.SpringConfig;
import org.example.service.UserService;
import org.example.service.impl.UserServiceImpl;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.All) // 运行所有模式
@Warmup(iterations = 5) //预热轮次
@Measurement(iterations = 5, time = 1)//基本的测试参数
@Threads(2) //线程数
@Fork(2) // 迭代集合进行的数目
@OutputTimeUnit(TimeUnit.SECONDS) //指定时间单位
@State(Scope.Thread) // 每个测试线程一个实例
public class SimpleBenchmark {

    private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    private UserService userService;

    @Param(value = {"100000", "1000000"})
    private String num;

    @Setup
    public void setup() {
//        client = Feign.builder().target(MyFeignClient.class, "http://example.com");
        userService = context.getBean(UserServiceImpl.class);
    }

    @Benchmark
    public void findAll() {
        userService.findAll(num);
    }

    public static void test() throws RunnerException {
        Options options = new OptionsBuilder()
                .include(SimpleBenchmark.class.getSimpleName())  //代表我要测试的是哪个类的方法
                .build();
        new Runner(options).run();
    }

    public static void main(String[] args) throws RunnerException {
        test();
    }

}
